package com.anysoft.me.utils;

import android.content.Context;
import android.view.Gravity;

import com.anysoft.base.BaseActivity;
import com.anysoft.linstener.OnNextLinstener;
import com.anysoft.view.dialog.IcbcDialog;

/**
 * Created by lty on 2017/12/3.
 * 提示框的帮助类
 */

public class AlertDialogHelper {

    public AlertDialogHelper() {
    }

    private static volatile AlertDialogHelper instance = null;

    public static AlertDialogHelper getInstance() {
        if (instance == null) {
            synchronized (AlertDialogHelper.class) {
                if (instance == null) {
                    instance = new AlertDialogHelper();
                }
            }
        }
        return instance;
    }

    private IcbcDialog showConfirmAlertDialog;

    public void showConfirmAlert(final Context context, String title,
                                 String content, String sbtn) {
        if (context instanceof BaseActivity){
            if (showConfirmAlertDialog == null && !((BaseActivity) context).isFinishing()) {
                showConfirmAlertDialog = new IcbcDialog(context);
                showConfirmAlertDialog.setColor("#FFFFFF");
                showConfirmAlertDialog.setCancelable(false);
                showConfirmAlertDialog.setAnimationEnable(true);
                showConfirmAlertDialog.setTitle(title);
                showConfirmAlertDialog.setContentText(content);
                showConfirmAlertDialog.setPositiveListener(sbtn, new IcbcDialog.OnPositiveListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        showConfirmAlertDialog.dismiss();
                        showConfirmAlertDialog = null;
                    }
                }).show();
            }
        } else {
            if (showConfirmAlertDialog == null) {
                showConfirmAlertDialog = new IcbcDialog(context);
                showConfirmAlertDialog.setColor("#FFFFFF");
                showConfirmAlertDialog.setCancelable(false);
                showConfirmAlertDialog.setAnimationEnable(true);
                showConfirmAlertDialog.setTitle(title);
                showConfirmAlertDialog.setContentText(content);
                showConfirmAlertDialog.setPositiveListener(sbtn, new IcbcDialog.OnPositiveListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        showConfirmAlertDialog.dismiss();
                        showConfirmAlertDialog = null;
                    }
                }).show();
            }
        }
    }

    public void showConfirmAlertWithLinstener(final Context context, String title,
                                              String content, String sbtn) {
        if (context instanceof BaseActivity){
            if (showConfirmAlertDialog == null && !((BaseActivity) context).isFinishing()) {
                showConfirmAlertDialog = new IcbcDialog(context);
                showConfirmAlertDialog.setColor("#FFFFFF");
                showConfirmAlertDialog.setCancelable(false);
                showConfirmAlertDialog.setAnimationEnable(true);
                showConfirmAlertDialog.setTitle(title);
                showConfirmAlertDialog.setContentText(content);
                showConfirmAlertDialog.setPositiveListener(sbtn, new IcbcDialog.OnPositiveListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        dialog.dismiss();
                        showConfirmAlertDialog = null;
                        if (mOnNextLinstener != null){
                            mOnNextLinstener.OndoConfirm();
                        }
                    }
                }).show();
            }
        } else {
            if (showConfirmAlertDialog == null ) {
                showConfirmAlertDialog = new IcbcDialog(context);
                showConfirmAlertDialog.setColor("#FFFFFF");
                showConfirmAlertDialog.setCancelable(false);
                showConfirmAlertDialog.setAnimationEnable(true);
                showConfirmAlertDialog.setTitle(title);
                showConfirmAlertDialog.setContentText(content);
                showConfirmAlertDialog.setPositiveListener(sbtn, new IcbcDialog.OnPositiveListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        dialog.dismiss();
                        showConfirmAlertDialog = null;
                        if (mOnNextLinstener != null){
                            mOnNextLinstener.OndoConfirm();
                        }
                    }
                }).show();
            }
        }

    }

    public void showConfirmAlert(final Context context, String title,
                                 String content, String sbtn1, String sbtn2) {
        if (context instanceof BaseActivity){
            if (showConfirmAlertDialog == null && !((BaseActivity) context).isFinishing()) {
                showConfirmAlertDialog = new IcbcDialog(context);
                showConfirmAlertDialog.setColor("#FFFFFF");
                showConfirmAlertDialog.setCancelable(false);
                showConfirmAlertDialog.setAnimationEnable(true);
                showConfirmAlertDialog.setContentTextGravity(Gravity.START);
                showConfirmAlertDialog.setTitle(title);
                showConfirmAlertDialog.setContentText(content);
                showConfirmAlertDialog.setEditTextType(false);
                showConfirmAlertDialog.setNegativeListener(sbtn1, new IcbcDialog.OnNegativeListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        dialog.dismiss();
                        showConfirmAlertDialog = null;
                        if (mOnNextLinstener != null){
                            mOnNextLinstener.OnConfirmNext();
                        }
                    }
                });
                showConfirmAlertDialog.setPositiveListener(sbtn2, new IcbcDialog.OnPositiveListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        dialog.dismiss();
                        showConfirmAlertDialog = null;
                        if (mOnNextLinstener != null){
                            mOnNextLinstener.OnCancelNext();
                        }
                    }
                });
                showConfirmAlertDialog.show();
            }
        } else{
            if (showConfirmAlertDialog == null) {
                showConfirmAlertDialog = new IcbcDialog(context);
                showConfirmAlertDialog.setColor("#FFFFFF");
                showConfirmAlertDialog.setCancelable(false);
                showConfirmAlertDialog.setAnimationEnable(true);
                showConfirmAlertDialog.setContentTextGravity(Gravity.START);
                showConfirmAlertDialog.setTitle(title);
                showConfirmAlertDialog.setContentText(content);
                showConfirmAlertDialog.setEditTextType(false);
                showConfirmAlertDialog.setNegativeListener(sbtn1, new IcbcDialog.OnNegativeListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        dialog.dismiss();
                        showConfirmAlertDialog = null;
                        if (mOnNextLinstener != null){
                            mOnNextLinstener.OnConfirmNext();
                        }
                    }
                });
                showConfirmAlertDialog.setPositiveListener(sbtn2, new IcbcDialog.OnPositiveListener() {
                    @Override
                    public void onClick(IcbcDialog dialog) {
                        dialog.dismiss();
                        showConfirmAlertDialog = null;
                        if (mOnNextLinstener != null){
                            mOnNextLinstener.OnCancelNext();
                        }
                    }
                });
                showConfirmAlertDialog.show();
            }
        }
    }

    public void showConfirmAlert(final Context context, String title,
                                 String content) {
        showConfirmAlert(context, title, content, "确定");
    }

    public void showConfirmAlert(final Context context, String content) {
        showConfirmAlert(context, "提示", content, "确定");
    }

    private OnNextLinstener mOnNextLinstener;

    public void setOnNextLinstener(OnNextLinstener onNextLinstener) {
        mOnNextLinstener = onNextLinstener;
    }

}
